package com.ruoyi.framework.satoken;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 权限、角色获取
 */
@Component
@Slf4j
public class StpInterfaceImpl implements StpInterface {

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        Set<String> permission = new HashSet<>();
        //在saSession 缓存中取出所有的权限
        Object o = StpUtil.getSessionByLoginId(loginId).get(SaSession.PERMISSION_LIST);
        if (o instanceof Set) {
            permission = (Set<String>) o;
        }

        return permission.stream().toList();
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        Set<String> role = new HashSet<>();
        //在saSession 缓存中取出所有的角色
        Object o = StpUtil.getSessionByLoginId(loginId).get(SaSession.ROLE_LIST);
        if (o instanceof Set) {
            role = (Set<String>) o;
        }

        return role.stream().toList();
    }
}
